package com.leyou.upload.controller;

import com.leyou.upload.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author by my
 * @Description TODO
 * @Date 2020/3/30 11:06
 */
@RestController
public class UploadController {
    @Autowired
    private UploadService uploadService;


    /**
     * 图片上传
     *
     * @param file 图片文件
     * @return url
     */
    @PostMapping("/image")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
        // 返回状态码和图片的url
        return ResponseEntity.ok(uploadService.upload(file));
    }

    /**
     * 获取阿里云OSS签名
     * @return json
     */
    @GetMapping("/signature")
    public ResponseEntity<Map<String, String>> signature() {
        // 获取签名并返回给前端
        return  ResponseEntity.ok(uploadService.signature());
    }
}
